package com.lc.user.express.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.lc.user.express.R;
import com.lc.user.express.app.AppContext;


/**
 * Created by Administrator on 2016/3/4.
 */
public class InPutDialog extends Dialog implements View.OnClickListener {
    private TextView tv_title, tv_input_key;
    private EditText et_input;
    private TextView tv_no, tv_ok;
    private DialogOnClickListener listener;
    private int max = 10000;

    public InPutDialog(Context context) {
        super(context, R.style.dialog1);
        // TODO Auto-generated constructor stub
        setContentView(R.layout.dialog_input);
        AppContext.ScaleScreenHelper.loadView((ViewGroup) getWindow().getDecorView());
        setCanceledOnTouchOutside(false);
        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_input_key = (TextView) findViewById(R.id.tv_input_key);
        et_input = (EditText) findViewById(R.id.et_input);
        tv_no = (TextView) findViewById(R.id.tv_no);
        tv_ok = (TextView) findViewById(R.id.tv_ok);
        tv_ok.setOnClickListener(this);
        tv_no.setOnClickListener(this);
        et_input.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(s.toString())) {
                    return;
                }
                if (Integer.valueOf(s.toString()) > max) {
                    et_input.setText(String.valueOf(max));
                }
            }
        });
    }

    public void setDialogOnClickListener(DialogOnClickListener listener) {
        this.listener = listener;
    }

    public void setTitle(String title) {
        tv_title.setText(title);
    }

    public void setInputKey(String str) {
        tv_input_key.setText(str);
    }

    public void setYes(String str) {
        tv_ok.setText(str);
    }

    public void setNo(String str) {
        tv_no.setText(str);
    }

    public void setText(CharSequence str) {
        et_input.setText(str);
    }

    public void setHint(CharSequence str) {
        et_input.setHint(str);
    }

    public void setMaxValue(int max) {
        this.max = max;
    }

    public void setMaxLength(int length) {
        et_input.setFilters(new InputFilter[]{new InputFilter.LengthFilter(length)});
    }

    /**
     * 设置确定按钮的点击事件
     *
     * @param isEnable
     */
    public void setButtonISEnabled(boolean isEnable) {
        tv_ok.setEnabled(isEnable);
    }

    public boolean getButtonClickable() {
        return tv_ok.isClickable();
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.tv_ok:
                if (listener != null) {
                    listener.Yes(et_input.getText().toString().trim());
                }
                break;
            case R.id.tv_no:
                dismiss();
                break;
        }
    }

    public interface DialogOnClickListener {
        void Yes(String temp);
    }
}
